{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras import layers,models,losses,metrics,callbacks\n",
    "import tensorflow.keras.backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import json\n",
    "json_log = open(\"./model/kears_log.josn\",mode='wt',buffering=1)\n",
    "json_logging_callbal = callbacks.LambdaCallback(\n",
    "    on_epoch_end=lambda epoch,logs:json_log.write(\n",
    "        json.dumps(dict(epoch=epoch,**logs)) + '\\n'\n",
    "    ),\n",
    "    on_train_end=lambda logs:json_log.close()\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-3-e20c3e8c2e37>, line 15)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;36m  File \u001B[1;32m\"<ipython-input-3-e20c3e8c2e37>\"\u001B[1;36m, line \u001B[1;32m15\u001B[0m\n\u001B[1;33m    \u001B[0m\n\u001B[1;37m    ^\u001B[0m\n\u001B[1;31mSyntaxError\u001B[0m\u001B[1;31m:\u001B[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "class LearningRateScheduler(callbacks.Callback):\n",
    "\n",
    "    def __init__(self, schedule, verbose=0):\n",
    "        super(LearningRateScheduler, self).__init__()\n",
    "        self.schedule = schedule\n",
    "        self.verbose = verbose\n",
    "\n",
    "    def on_epoch_begin(self, epoch, logs=None):\n",
    "        if not hasattr(self.model.optimizer, 'lr'):\n",
    "            raise ValueError('Optimizer must have a \"lr\" attribute.')\n",
    "        try:\n",
    "            lr = float(K.get_value(self.model.optimizer.lr))\n",
    "            lr = self.schedule(epoch, lr)\n",
    "        except TypeError:  # Support for old API for backward compatibility\n",
    "            lr = self.schedule(epoch)\n",
    "        if not isinstance(lr, (tf.Tensor, float, np.float32, np.float64)):\n",
    "            raise ValueError('The output of the \"schedule\" function '\n",
    "                             'should be float.')\n",
    "        if isinstance(lr, ops.Tensor) and not lr.dtype.is_floating:\n",
    "            raise ValueError('The dtype of Tensor should be float')\n",
    "        K.set_value(self.model.optimizer.lr, K.get_value(lr))\n",
    "        if self.verbose > 0:\n",
    "            print('\\nEpoch %05d: LearningRateScheduler reducing learning '\n",
    "                 'rate to %s.' % (epoch + 1, lr))\n",
    "\n",
    "    def on_epoch_end(self, epoch, logs=None):\n",
    "        logs = logs or {}\n",
    "        logs['lr'] = K.get_value(self.model.optimizer.lr)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}